package org.kjtc.controller;

import org.kjtc.entity.UserInfo;
import org.kjtc.service.UserService;
import org.kjtc.web.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.security.SecurityProperties;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/12/11 0011.
 */
@RestController
public class UserController extends BaseController {

    @Autowired
    private UserService userService;

    @RequestMapping(value="doGetAllUser")
    public JsonResult doGetAllUser(){
       List<UserInfo> list=userService.getAllUsers();
        return new JsonResult(list);
    }

    @RequestMapping(value = "/updateUserInfo")
    public Map<String, Object> updateUser(UserInfo userInfo) {
        UserInfo u= (UserInfo) this.getSession().getAttribute("userInfo");
        String userID=u.getUserID();
        if(userID != null && userID != ""){
            userInfo.setUpdateUser(userID);
        }
        Map<String, Object> map = new HashMap<String, Object>();
        int count = 0;
        try {
            count = userService.updateUserInfo(userInfo);
            if (count > 0) {
                map.put("result", "Success");
            } else {
                map.put("result", "NotFound");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return map;
        }
        map.put("result", "Success");
        return map;
    }

    @RequestMapping(value="addUserInfo")
    public Map<String, Object> addUser(UserInfo userInfo) {
        UserInfo u= (UserInfo) this.getSession().getAttribute("userInfo");
        String userID=u.getUserID();
        if(userID != null && userID != ""){
            userInfo.setCreateUser(userID);
            userInfo.setUpdateUser(userID);
        }
        Map<String, Object> map = new HashMap<String, Object>();
        int count = 0;
        try {
            count = userService.addUserInfo(userInfo);
            if (count > 0) {
                map.put("result", "Success");
            } else {
                map.put("result", "NotFound");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return map;
        }
        map.put("result", "Success");
        return map;
    }
    @RequestMapping(value="doGetAllUserID")
    public JsonResult doGetAllUserID(UserInfo userInfo){
        int num=userService.getAllUserID(userInfo);
        System.out.println("num=== "+num);
        return new JsonResult(num);
    }

}
